<template>
  <Actionsheet :actions="actions" v-model="sheetVisible"></Actionsheet>
</template>
<script>
import 'mint-ui/lib/style.css'
import { MessageBox, Actionsheet } from "mint-ui";
 import { Dialog } from 'vant';
export default {
  components: { Actionsheet },
  props: {
    value: {
      type: Boolean,
      //default: false,
      required: true
    },
    tencentKey: {
      type: String,
      //default: "FABBZ-K4T6W-MLNRI-RPAMH-SHHYT-7AFKP",
      required: true
    },
    from: {
      type: Object,
      //   default: () => {
      //     return {
      //       name: "杨家坪", //起点名称
      //       lat: "29.5079160000", //纬度
      //       lng: "106.5139370000" //经度
      //     };
      //   },
      required: true
    },
    to: {
      type: Object,
      //   default: () => {
      //     return {
      //       name: "解放碑", //终点名称
      //       lat: "29.5573000000", //纬度
      //       lng: "106.5771500000" //经度
      //     };
      //   },
      required: true
    }
  },
  data() {
    return {
      sheetVisible: false,
      actions: [],
      Android: {
        gd: {
          target: "com.autonavi.minimap",
          url:
            "http://gdown.baidu.com/data/wisegame/19f3a5b0d018ff42/gaodeditu_8251.apk"
        },
        bd: {
          target: "com.baidu.BaiduMap",
          url:
            "http://m.gdown.baidu.com/07962ff5d6016858f43cbc6035482c543451b01f09a3d6668dbdc6be6701fea6bc8e5cb4ce55cd75ecb2318ec56dd9c20aa4608ba4adb8a02d8f20d42d875a009effe61d097fe685b28fe8f9387b3bd313809f8c37646b6b6f7884c872d24b45711e13289b0a636b7d7d05e28222ff5b72080cb623e6f8d7380b7d37c092f81317b6a4f6b44ddb05422217f6cd972247"
        },
        tx: {
          target: "com.tencent.map",
          url:
            "http://gdown.baidu.com/data/wisegame/d40ca8ba19dc5459/tengxunditu_675.apk"
        }
      },
      iOS: {
        gd: {
          target: "iosamap://",
          url: "itms-apps://itunes.apple.com/cn/app/id461703208?mt=8"
        },
        bd: {
          target: "baidumap://",
          url: "itms-apps://itunes.apple.com/cn/app/id452186370?mt=8"
        },
        tx: {
          target: "qqmap://",
          url: "itms-apps://itunes.apple.com/cn/app/id481623196?mt=8"
        },
        pg: {
          target: "http://maps.apple.com/",
          url: "itms-apps://itunes.apple.com/cn/app/id915056765?mt=8"
        }
      },
      names: {
        gd: "高德地图",
        bd: "百度地图",
        tx: "腾讯地图",
        pg: "Apple地图"
      }
    };
  },
  mounted() {
    this.actions.push(
      ...[
        {
          name: "高德地图",
          method: () => {
            this.handleNavigation("gd");
          }
        },
        {
          name: "百度地图",
          method: () => {
            this.handleNavigation("bd");
          }
        },
        {
          name: "腾讯地图",
          method: () => {
            this.handleNavigation("tx");
          }
        }
      ]
    );
    if (plus.os.name === "iOS") {
      this.actions.push({
        name: "Apple地图",
        method: () => {
          this.handleNavigation("pg");
        }
      });
    }
  },
  watch: {
    sheetVisible: function(val) {
      if (val === false) {
        this.$emit("input", false);
      }
    },
    value: function(val) {
      if (val === true) {
        this.sheetVisible = true;
      }
    }
  },
  methods: {
    handleNavigation(type) {
      if (this.installed(type) === false) {
        Dialog.confirm({
                  title: '提示',
                  message: `未安装${this.names[type]}，是否前往下载安装？`,
                    })
          .then(action => {
            this.download(type);
          })
          .catch(err => {
            this.sheetVisible = false;
          });
      } else {
        var tag = "";
        switch (type) {
          case "gd":
            if (plus.os.name === "Android") {
              tag = `amapuri://route/plan/?sid=&slat=${this.from.lat}&slon=${this.from.lng}&sname=${this.from.name}&did=&dlat=${this.to.lat}&dlon=${this.to.lng}&dname=${this.to.name}&dev=0&t=0`;
            } else {
              tag = `iosamap://path?sourceApplication=applicationName&sid=&slat=${this.from.lat}&slon=${this.from.lng}&sname=${this.from.name}&did=&dlat=${this.to.lat}&dlon=${this.to.lng}&dname=${this.to.name}&dev=0&t=0`;
            }
            break;
          case "bd":
            tag = `baidumap://map/direction?origin=name:${this.from.name}|latlng:${this.from.lat},${this.from.lng}&destination=name:${this.to.name}|latlng:${this.to.lat},${this.to.lng}&coord_type=bd09ll&target=1&src=cn.lxmou.mapnav`;
            break;
          case "tx":
            tag = `qqmap://map/routeplan?type=drive&from=${this.from.name}&fromcoord=${this.from.lat},${this.from.lng}&to=${this.to.name}&tocoord=${this.to.lat},${this.to.lng}&referer=${this.tencentKey}`;
            break;
          case "pg":
            tag = `http://maps.apple.com/?saddr=${this.from.name}&daddr=${this.to.name}`;
            break;
          default:
            break;
        }
        if (plus.os.name === "Android") {
          //var Intent = plus.android.importClass("android.content.Intent");
          //var intent=Intent.getIntent(`amapuri://route/plan/?sid=&slat=39.92848272&slon=116.39560823&sname=A&did=&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&t=0`);//高德
          //var intent=Intent.getIntent(`baidumap://map/direction?origin=name:对外经贸大学|latlng:39.98871,116.43234&destination=西直门&coord_type=bd09ll&target=1&src=andr.baidu.openAPIdemo`);//百度
          //var intent=Intent.getIntent(`qqmap://map/routeplan?type=drive&from=清华&fromcoord=39.994745,116.247282&to=怡和世家&tocoord=39.867192,116.493187&referer=${this.tencentKey}`);//QQ
          //var main = plus.android.runtimeMainActivity();
          //main.startActivity(intent);
          plus.runtime.openURL(
            encodeURI(tag),
            err => {
              alert(JSON.stringify(err));
            },
            this.getTarget(type)
          );
        } else {
          plus.runtime.openURL(encodeURI(tag), err => {
            alert(JSON.stringify(err));
          });
        }
      }
    },
    getTarget(type) {
      return eval(`this.${plus.os.name}['${type}'].target`);
    },
    getUrl(type) {
      return eval(`this.${plus.os.name}['${type}'].url`);
    },
    download(type) {
      if (plus.os.name === "iOS") {
        plus.runtime.openURL(this.getUrl(type));
      }
      if (plus.os.name === "Android") {
        plus.runtime.openURL(this.getUrl(type));
      }
    },
    installed(type) {
      var target = this.getTarget(type);
      if (plus.os.name === "Android") {
        try {
          var packageName = target; //百度地图的包名
          var main = plus.android.runtimeMainActivity();
          var packageManager = main.getPackageManager();
          var PackageManager = plus.android.importClass(packageManager);
          var packageInfo = packageManager.getPackageInfo(
            packageName,
            PackageManager.GET_ACTIVITIES
          );
          if (packageInfo) {
            return true;
          } else {
            return false;
          }
        } catch (e) {
          return false;
        }
      }
      if (plus.os.name === "iOS") {
        var UIApplication = plus.ios.importClass("UIApplication");
        var NSURL = plus.ios.importClass("NSURL");

        var app = UIApplication.sharedApplication();
        var bdScheme = NSURL.URLWithString(target);
        var install = app.canOpenURL(bdScheme);

        plus.ios.deleteObject(bdScheme);
        plus.ios.deleteObject(app);
        return install;
      }
    }
  }
};
</script>
